<?php

class myUser extends sfBasicSecurityUser
{
	const NAMESPACE_USER     = 'FRONT';

	public function connect($oUser)
	{
		// Sauvegarde de l'ID
		$this->setAttribute('id', $oUser->id, self::NAMESPACE_USER);

		// authenticate user
        $this->setAuthenticated(true);
        $this->addCredential($oUser->type);
	}
	
	public function getUser()
	{
		if (!$this->isAuthenticated())
			return false;

		$iId     = $this->getAttribute('id', null, self::NAMESPACE_USER);
		$oUser	 = null;
		
		if (!$iId || !$oUser = Doctrine::getTable('Users')->find($iId))
		{
			$this->setAuthenticated(false);
			return false;
		}
		return $oUser;
	}
	
	public function login($sLogin, $sMdp)
	{
		$oUser = Doctrine::getTable('Users')
				->createQuery('u')
				->where('login = ? and mdp = ?', array($sLogin, md5($sMdp)))
				->fetchOne();
		if (empty($oUser))
			return false;
		
		$this->connect($oUser);
		return true;
	}
}
